<template>
	<view class="content has-tabbar">
		<!-- <view class="index-banner padding-x-32">
			<swiper class="swiper" autoplay @change="handleChange" :indicator-dots="false" circular :interval="interval" :duration="duration">
				<swiper-item v-for="(item, index) in imgList" :key="index" class="imgItem">
					<image class="search-icon" style="border-radius: 8rpx;" src="/static/images/banner.png"></image>
				</swiper-item>
		
			</swiper>
			<view class="dot-list ta-c">
				<view v-for="(item, index) in imgList" :key="index" class="dot-li" :class="{'active': current == index}"></view>
			</view>
		</view> -->

		<!-- <view v-if="noticeList.length > 0" class="notice-content flexbox flexbox-middle">
			<image class="new-icon" src="/static/images/new_icon.png"></image>
			 <view class="flexchild">
				<swiper class="swiper" vertical="true" autoplay="false" circular disable-touch :duration="500"  :interval="2500">
				    <swiper-item v-for="(item, index) in noticeList" :key="index" catchtouchmove='catchTouchMove'>
					      <view class="f-28 ellipsis">{{item}}</view>
					</swiper-item>
				</swiper>
			</view>
		</view> -->
		<image class="back-img" src="/static/images/bg-1.png"></image>
		<view class="index-container">
			<view class="toos">
				<view class="title flexbox flexbox-middle"><view class="gan"></view>常用工具</view>
				<view class="tools-cnt flexbox">
					<view class="flexchild tools-item">
						<button v-if="!ifLogin" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"></button>
						<view v-else @click="goto('/pages/myWorcard/index', true)" class="login-back"></view>
						<image class="tool-icon" src="/static/images/tool2.png"></image>
						<view class="name f-32">出示证件</view>
						<view class="f-24 text">出示我的工作证</view>
					</view>
					<view @click="scancode" class="flexchild tools-item">
						<image class="tool-icon" src="/static/images/tool1.png"></image>
						<view class="name f-32">扫一扫</view>
						<view class="f-24 text">查验他人工作证</view>
					</view>
				</view>
			</view>
			
			<view class="toos">
				<view class="title flexbox flexbox-middle"><view class="gan"></view>更多内容</view>
				<view class="adv-box">
					<image v-for="(item, index) in banners" :key="index" @click="gotoAdv(item)" class="adv-img" :src="baseurl + item.banner_img"></image>
				</view>
			</view>
		</view>
		
		
		 <!-- <button @click="testget()">测试企业认证按钮</button> -->
		<tab-bar active="1"></tab-bar>
		<login-alert :isShow="loginShow" @loginSuccess="loginSuccess" @closeLogin="closeLogin"/>
	</view>

</template>

<script>
	import tabBar from '@/components/tabBar/tabBar.vue'
	import loginAlert from '@/components/loginAlert/index.vue'
	import config from "../../request/config.js";
	import { scancode, phoneLogin, handlerQuery, pushData, getURLParamInfo } from '@/static/js/utils.js'
	import {
		// mapState,  
		mapMutations
	} from 'vuex';
	export default {
		components: {
			tabBar,
			loginAlert
		},
		// computed: {  
		//           ...mapState(['userInfo'])
		//       },
		data() {
			return {
				code: '', //微信临时登录凭证
				noticeList: [],
				scrollTop:0,
				tabList: ['查企业', '验证书'],
				tabCurrent: 0,
				duration: 500,
				interval: 3000,
				current: 0,
				imgList: ['1'],
				loginFlag: false,
				ifLogin: false,
				banners: [],
				baseurl: '',
				scene: '',
				scan_flag: '',
				company_id: '',
				loginShow: false
			}
		},
		onLoad: function(e) {
			this.scan_flag = e.scan_flag || ''
			this.company_id = e.company_id || ''
			if (e.q) {
				let strArray = decodeURIComponent(e.q)
				this.scan_flag = getURLParamInfo('scan_flag', strArray)
				this.company_id = getURLParamInfo('company_id', strArray)
			}
			this.scene = this.scan_flag || this.company_id || ''
			if(this.scan_flag) {
				// 不管有没有登录都要请求
				this.setLoginFlag(this.scan_flag)
			}
			if (uni.getStorageSync('authorization') && this.scene) {
				this.$HTTP.post('/wechat/userinfo', {noRedirectLogin: 1}, false).then((res) => {
					this.getparam(res.data)
					this.userinfo = res.data
				}).catch(err => {
					if(err.code == 4001) {
						this.loginShow = true
					}
				})
				
			}
			this.baseurl = config.BASEURL
			this.getAdv()
		},
		onShow() {
			if (uni.getStorageSync('authorization')) {
				this.ifLogin = true
				this.loginShow = false
			} else {
				this.ifLogin = false
				this.loginShow = true
			}
			uni.login({
				success: function(loginRes) {	
				}
			})
		},
		methods: {
			getparam(res) {
				if (this.scene) {
					let role = res.role;
					if (this.scan_flag){
						uni.setStorageSync('role', role);
						if (role == 'user') {
							uni.redirectTo({
								url: `/pages/openWz-step1/index?type=companyAuth&scan_flag=${this.scan_flag}`
							});
							return false
						}
						if (role == 'real_user') {
							uni.redirectTo({
							    url: `/pages/companyAuth/index?scan_flag=${this.scan_flag}`
							})
							return false
						}
						this.updateRole(this.scan_flag)
					}
					if (this.company_id){
						if (role == 'user') {
							uni.redirectTo({
								url: `/pages/openWz-step1/index?type=authOperation&id=${this.company_id}`
							});
							return false
						}
						this.bindCompany(this.company_id)
					}
				}
			},
			//设置扫登录码标识
			setLoginFlag(scan_flag) {
				let param= {
					scan_flag:scan_flag
				}
				this.$HTTP.post('/user/scan_code', param, false).then((res) => {
					/*uni.showToast({
						title: res.msg,
						icon: 'none'
					})*/
				}).catch((err) => {
					
				})
			},
			loginSuccess(res){
				this.ifLogin = true
				this.loginShow = false
				if(this.scene) {
					this.getparam(res)
				} else {
					if (res.role == 'user') {
						uni.navigateTo({
							url: '/pages/openWz-step1/index'
						});
					}
				}
			},
			closeLogin() {
				this.loginShow = false
			},
			updateRole(scan_flag) {
				pushData(scan_flag)
			},
			gotoAdv(item) {
				switch(item.banner_type) {
					case '0':
						break;
					case '1':
						uni.navigateTo({
							url: `/${item.banner_link}`
						});
						break; 
					case '2':
						uni.navigateTo({
							url: `/pages/webview/index?url=${item.banner_link}`
						});
						break;
					case '3':
						// 只有在小程序编译
						//#ifdef MP
							uni.setClipboardData({
								data: item.banner_link,
								success: function () {
									uni.showToast({
										title: '内容已复制到剪贴板，请到其它浏览器打开',
										duration: 2000,
										icon: 'none'
									});
								}
							});
						//#endif
						break; 
					default: 
					
				}
			},
			bindCompany(company_id) {
				let param = {
					company_id: company_id
				}
				this.$HTTP.post('/user/binded', param, true).then((res) => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}).catch((err) => {
					
				})
			},
			/**
			 * 检测广告轮播图滚动
			 */
			handleChange(e) {
				this.current = e.detail.current
			},
			///...mapMutations(['login']),
			getPhoneNumber: function(e) {
				let that = this
				phoneLogin(e, function(res) {
					that.ifLogin = true
					uni.navigateTo({
						url: '/pages/myWorcard/index'
					});
				}, function(){})
			},
			getAdv() {
				let param = {
					type: 1
				}
				this.$HTTP.get('/image/banner', param, true).then((res) => {
					this.banners = res.data.banner
				}).catch(err => {
				
				})
			},
			scancode() {
				scancode(this)
			},
			
			goto(url, needLogin) {
				if (!needLogin) {
					uni.navigateTo({
						url: url
					});
				} else {
					if (uni.getStorageSync('authorization')) {
						uni.navigateTo({
							url: url
						});
					} else {
						uni.showToast({
						    title: '请先登录',
						    icon: 'none'
						})
						setTimeout(() => {
							uni.navigateTo({
								url: `/pages/login/index?rurl=${encodeURIComponent(url)}&noAuth=1`
							});
						}, 2000)
						
					}
					
				}
				
			}
		},
		onShareAppMessage(res) {
		    return {
		      //imageUrl: this.shareImgUrl,
			  title: '可信工作证',
			  path: '/pages/index/index?isShare=1',
			  success: function(res){
				  uni.showToast({
				  	title: "分享成功",
				  	icon: "none"
				  });
			  }
		    }
		}
	}
</script>

<style lang="scss" scoped>
	@import "./index.scss";
</style>